﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dao;
using System.Data.SqlClient;

namespace 班级系统
{
    public partial class Form4 : Form
    {
        Form frm4;
        Student thou = new Student();
        public Form4(Form frm4, Student thou)
        {
            InitializeComponent();
            this.frm4 = frm4;
            this.thou = thou;
        }
        /// 
        /// 返回前一个窗体
        /// 


        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            frm4.Show();
        }
        int[] ischeck = new int[4]; //利用一个数组判断有没有选择

        private void Form4_Load(object sender, EventArgs e)
        {
            comboBox1.Text = this.comboBox1.Items[0].ToString();//显示第一行
            comboBox2.Text = this.comboBox2.Items[0].ToString();
            for (int i = 0; i < 4; i++)
            {
                ischeck[i] = 0;
            }

            try
            {
                checkBox1.Text = thou.listView.Items[0].Text;
                checkBox2.Text = thou.listView.Items[1].Text;
                checkBox3.Text = thou.listView.Items[2].Text;
                checkBox4.Text = thou.listView.Items[3].Text;
            }
            catch (Exception)
            {
            }
            //初始化数据 

            //foreach (Control item in groupBox1.Controls)
            //{
            //    if (item.Text=="checkBox1")
            //    {
            //        groupBox1.Controls.Remove(item);
            //    }
            //}
            if (checkBox2.Text == "1")
            {
                groupBox1.Controls.Remove(checkBox2);
            }
            if (checkBox3.Text == "1")
            {
                groupBox1.Controls.Remove(checkBox3);
            }
            if (checkBox4.Text == "1")
            {
                groupBox1.Controls.Remove(checkBox4);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string[] from4s = new string[2];
            from4s[0] = comboBox1.Text;
            from4s[1] = comboBox2.Text;
            thou.Form4S = from4s;
            thou.checkB1 = checkBox1;
            thou.checkB2 = checkBox2;
            thou.checkB3 = checkBox3;
            thou.checkB4 = checkBox4;
            thou.ischecked = ischeck;
            //保存成功跳轉
            if (saveAll())
            {
                Conclud frm3 = new Conclud(thou);
                frm3.Show();
                this.Hide();
            }
          
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                ischeck[0] = 1;
            }

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                ischeck[1] = 1;
            }

        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked == true)
            {
                ischeck[2] = 1;
            }

        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked == true)
            {
                ischeck[3] = 1;
            }
        }
        public bool saveAll()
        {
            //校验数据
            string className = thou.Form1S[2].ToString();
            if (string.IsNullOrEmpty(className))
            {
                MessageBox.Show("教室不存在");
                return false;
            }
            if (thou.listView.Items.Count == 0)
            {
                MessageBox.Show("学生不存在");
                return false;
            }
            // 更新学生信息
            List<string> name = new List<string>();
            if (thou.checkB1.Text != "1" && thou.ischecked[0] == 1)
            {
                name.Add(thou.checkB1.Text);
            }
            if (thou.checkB2.Text != "1" && thou.ischecked[1] == 1)
            {
                name.Add(thou.checkB2.Text);
            }
            if (thou.checkB3.Text != "1" && thou.ischecked[2] == 1)
            {
                name.Add(thou.checkB3.Text);
            }
            if (thou.checkB4.Text != "1" && thou.ischecked[3] == 1)
            {
                name.Add(thou.checkB4.Text);
            }
            for (int i = 0; i < thou.listView.Items.Count; i++)
            {
                string sNum = thou.listView.Items[i].SubItems[1].Text;
                string sName = thou.listView.Items[i].Text;
                string status = "0";
                //考勤員
                if (name.Contains(sName))
                {
                    status = "1";
                    thou.listView.Items[i].SubItems[5].Text = "1";
                }
                else {
                    thou.listView.Items[i].SubItems[5].Text = "0";
                }
                string sAge = thou.listView.Items[i].SubItems[2].Text;
                string sSex = thou.listView.Items[i].SubItems[3].Text;
                string sPlace = thou.listView.Items[i].SubItems[4].Text;
               
                string classId = findClassId(className);
                string sql = "select * from student where student_num = @snum";
                SqlParameter[] para = new SqlParameter[]{
                    new SqlParameter("snum",sNum)
                };
                DataTable dt = sqlcontent.dt(sql, para);
                string update = "";
                //若存在則更新  不存在則插入數據
                if (dt.Rows.Count > 0)
                {
                    update = "update student set student_name=@sName,"
                    + "student_sex=@sSex,student_age=@sAge,student_place=@sPlace,status=@status "
                    + "where student_num=@sNum";
                }
                else
                {
                    update = "insert into student"
                   + "(student_name,student_num,student_sex,student_age,student_place,status,class_id)"
                   + "values(@sName,@sNum,@sSex,@sAge,@sPlace,@status,@classId)";
                }
                SqlParameter[] spar = new SqlParameter[]{
                    new SqlParameter("sNum",sNum),
                    new SqlParameter("sName",sName),
                    new SqlParameter("sSex",sSex),
                    new SqlParameter("sAge",sAge),
                    new SqlParameter("sPlace",sPlace),
                    new SqlParameter("status",status),
                    new SqlParameter("classId",classId)
                };
                if (sqlcontent.content(update, spar) != 0)
                {
                    //MessageBox.Show("保存成功");
                }
                else
                {
                    MessageBox.Show("学生信息保存出错！");
                    return false;
                }
            }


            // 更新班级信息
            string cSql = "select * from class where class_name=@className";
            string sql_logo="select logo_id from logo where logo_url=@logo_url";
            string logo_url=thou.Form3S[2];
            SqlParameter[] cPara = new SqlParameter[]{
                    new SqlParameter("className",className),
                    new SqlParameter("logo_url",logo_url)
                };
            DataTable cdt = sqlcontent.dt(cSql, cPara);
            DataTable dt_logo=sqlcontent.dt(sql_logo,cPara);
           
            string cUpdate = "";
            cUpdate = "update class set class_call=@classCall,class_song=@classSong,"
            +"class_study_place=@classPlace,class_study_time=@cTime,logo_id=@logoId "
            +"where class_name=@className";
         
          
            string logoId=dt_logo.Rows[0][0].ToString();
            string classCall = thou.Form3S[1];
            string classSong = thou.Form3S[0];
            string classPlace = thou.Form4S[1];
            string cTime = thou.Form4S[0];
            cPara = new SqlParameter[]{
                   new SqlParameter("classCall",classCall),
                   new SqlParameter("classSong",classSong),
                   new SqlParameter("classPlace",classPlace),
                   new SqlParameter("cTime",cTime),
                   new SqlParameter("className",className),
                   new SqlParameter("logoId",logoId)
                };
            if (sqlcontent.content(cUpdate, cPara) == 0)
            {
                MessageBox.Show(conString.errstr);
                return false;
            }

            MessageBox.Show("保存成功");
            return true;
        }
        public string findClassId(string className) {
            string sql = "select class_id from class where class_name=@className";
            SqlParameter[] para = new SqlParameter[]{
                new SqlParameter("className",className)
            };
            DataTable dt = sqlcontent.dt(sql, para);
            string classId= dt.Rows[0][0].ToString();
            return classId;
        }
    }
}
